package com.qingyun.modular.order.service.impl;

import com.qingyun.common.controller.BaseFrontController;
import com.qingyun.common.util.ExcelFormatUtil;
import com.qingyun.modular.order.service.IExportService;
import com.qingyun.modular.order.vo.OrdersVo;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ExportServiceImpl implements IExportService {
    Logger logger = LoggerFactory.getLogger(ExportServiceImpl.class);

    @Override
    public ResponseEntity<byte[]> exportOrdersExcel(HttpServletRequest request, HttpServletResponse response, List<OrdersVo> list) {
        try {
            logger.info(">>>>>>>>>>开始导出excel>>>>>>>>>>");
            BaseFrontController baseFrontController = new BaseFrontController();
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHMMSS");
            String fileName = df.format(new Date());
            return baseFrontController.buildResponseEntity(export((List<OrdersVo>) list), fileName + "_导出汇总列表.xls");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(">>>>>>>>>>导出excel 异常，原因为：" + e.getMessage());
        }
        return null;
    }

    private InputStream export(List<OrdersVo> list) {
        logger.info(">>>>>>>>>>>>>>>>>>>>开始进入导出方法>>>>>>>>>>");
        ByteArrayOutputStream output = null;
        InputStream inputStream1 = null;
        SXSSFWorkbook wb = new SXSSFWorkbook(1000);// 保留1000条数据在内存中
        SXSSFSheet sheet = (SXSSFSheet) wb.createSheet();//添加一个页
        // 设置报表头样式
        CellStyle header = ExcelFormatUtil.headSytle(wb);// cell样式
        CellStyle content = ExcelFormatUtil.contentStyle(wb);// 报表体样式

        // 每一列字段名
        String[] strs = new String[]{"订单ID", "分销商ID", "店铺名称", "订单编号", "分销订单类型","订单状态", "预计结算日期","结算状态","商品名称","商品价格", "分销商姓名","分销商等级","佣金比例","提成方式","佣金收益","积分奖励"};

        // 字段名所在表格的宽度
        int[] ints = new int[]{2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000,2000};

        // 设置表头样式
        ExcelFormatUtil.initTitleEX(sheet, header, strs, ints);
        logger.info(">>>>>>>>>>>>>>>>>>>>表头样式设置完成>>>>>>>>>>");

        if (list != null && list.size() > 0) {
            logger.info(">>>>>>>>>>>>>>>>>>>>开始遍历数据组装单元格内容>>>>>>>>>>");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (int i = 0; i < list.size(); i++) {
                OrdersVo vo = list.get(i);
                Row row = sheet.createRow(i + 1);
                int j = 0;

                SXSSFCell cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(vo.getId()); // 订单ID
                cell.setCellStyle(content);

//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getUserId()); // 分销商ID
//                cell.setCellStyle(content);
//
//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getShopName()); // 店铺名称
//                cell.setCellStyle(content);
//
//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getOrderNo()); // 订单编号
//                cell.setCellStyle(content);
//
//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getOrderType()); // 分销订单类型
//                cell.setCellStyle(content);
//
//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getOrderStatus()); // 订单状态
//                cell.setCellStyle(content);
//
//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getSettlementAt()); // 预计结算日期
//                cell.setCellStyle(content);
//
//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getSettlementStatus()); // 结算状态
//                cell.setCellStyle(content);
//
//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getGoodsName()); // 商品名称
//                cell.setCellStyle(content);
//
//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getPrice()); // 商品价格
//                cell.setCellStyle(content);
//
//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getPrice()); // 分销商姓名
//                cell.setCellStyle(content);
//
//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getPrice()); // 分销商等级
//                cell.setCellStyle(content);
//
//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getPrice()); // 佣金比例
//                cell.setCellStyle(content);
//
//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getPrice()); // 提成方式
//                cell.setCellStyle(content);
//
//                cell = (SXSSFCell) row.createCell(j++);
//                cell.setCellValue(vo.getPrice()); // 佣金收益
//                cell.setCellStyle(content);

                cell = (SXSSFCell) row.createCell(j++);
                cell.setCellValue(vo.getPrice()); // 积分奖励
                cell.setCellStyle(content);
            }
            logger.info(">>>>>>>>>>>>>>>>>>>>结束遍历数据组装单元格内容>>>>>>>>>>");
        }
        try {
            output = new ByteArrayOutputStream();
            wb.write(output);
            inputStream1 = new ByteArrayInputStream(output.toByteArray());
            output.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null) {
                    output.close();
                    if (inputStream1 != null)
                        inputStream1.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return inputStream1;
    }

}